package com.bawei.module_moto_circle.ui.search.adapter

import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_moto_circle.R
import com.bawei.module_moto_circle.databinding.LayoutSearchSuggestionItemBinding
import com.bawei.module_moto_circle.model.SearchKeywordModelItem

/**
 *作者：lxy
 *功能:
 *时间：2025/6/5
 */
class SearchSuggestionAdapter(private val onClickSuggestionItem: (SearchKeywordModelItem) -> Unit) :
    RecyclerView.Adapter<SearchSuggestionViewHolder>() {

    private val suggestionList: MutableList<SearchKeywordModelItem> = mutableListOf()
    private lateinit var mContext: Context
    private var highlightWord: String = ""
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchSuggestionViewHolder {
        mContext = parent.context
        val binding = DataBindingUtil.inflate<LayoutSearchSuggestionItemBinding>(
            LayoutInflater.from(mContext),
            R.layout.layout_search_suggestion_item, parent, false
        )
        return SearchSuggestionViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return suggestionList.size
    }

    override fun onBindViewHolder(holder: SearchSuggestionViewHolder, position: Int) {
        val suggestionItem = suggestionList[position]
        if (suggestionItem.socialType == 1) {
            handleTopicSuggestion(holder, suggestionItem)
        } else {
            handleHighlightSuggestion(holder, suggestionItem)
        }
    }

    fun setData(list: List<SearchKeywordModelItem>) {
        this.suggestionList.clear()
        this.suggestionList.addAll(list)
        notifyDataSetChanged()
    }

    fun clearData() {
        this.suggestionList.clear()
        notifyDataSetChanged()
    }

    fun setHighlightWord(keyword: String) {
        this.highlightWord = keyword
        notifyDataSetChanged()
    }

    //处理话题的搜索建议
    private fun handleTopicSuggestion(
        holder: SearchSuggestionViewHolder,
        suggestion: SearchKeywordModelItem
    ) {
        val content = String.format("#%s#", suggestion.keyword)
        val spannableStringBuilder = SpannableStringBuilder(content)
        spannableStringBuilder.setSpan(
            ForegroundColorSpan(
                ContextCompat.getColor(
                    mContext,
                    com.bawei.lib_common.R.color.topic
                )
            ), 0, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        holder.binding.tv.text = spannableStringBuilder
    }

    //高亮处理
    private fun handleHighlightSuggestion(
        holder: SearchSuggestionViewHolder,
        suggestion: SearchKeywordModelItem
    ) {

        holder.binding.tv.text = (suggestion.keyword ?: "").toString().trim().replace("\r|\n", "")
    }
}

class SearchSuggestionViewHolder(val binding: LayoutSearchSuggestionItemBinding) :
    RecyclerView.ViewHolder(binding.root) {

}